package com.wzu.controller;

import com.wzu.entity.Menu;
import com.wzu.entity.Result;
import com.wzu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {
    
    @Autowired
    private MenuService menuService;
    
    // 查询用户权限信息
    @GetMapping("/search/{userid}")
    public Result getMenuByUserId(@PathVariable String userid) {
        Menu menu = menuService.getMenuByUserId(userid);
        if (menu != null) {
            return new Result("查询成功", menu, 200);
        }
        return new Result("未找到该用户的权限信息", null, 404);
    }
    
    // 更新用户权限信息
    @PutMapping("/update/{userid}")
    public Result updateMenu(@PathVariable String userid, @RequestBody Menu menu) {
        // 验证路径参数与请求体中的userid是否匹配
        if (!userid.equals(menu.getUserid())) {
            return new Result("用户ID不匹配", null, 400);
        }
        
        if (menuService.updateMenu(menu)) {
            return new Result("权限更新成功", null, 200);
        }
        return new Result("权限更新失败", null, 400);
    }
} 